
package view;

import util.BookData;
import util.BookUtil;

import com.oahc.bookreaderone.MainActivity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;

public class BookView extends View {

    Bitmap mCurPageBitmap = null; // 当前页

    Bitmap mNextPageBitmap = null, mTempPageBitmap = null;

    BookPageFactory _bpg;

    int screenW=480;
    public BookView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BookView(Context context) {
        super(context);
    }

    public void setBpg(BookPageFactory bpg) {
        _bpg = bpg;
    }

    View curParent;

    BookMenuView bookMenuView;

    public void setParentView(View parent) {
        curParent = parent;
        bookMenuView = new BookMenuView(curParent);
        screenW=MainActivity.getInstance().getScreenWidth();
    }

    public void setBitmap(Bitmap c_bmp, Bitmap n_bmp) {
        mCurPageBitmap = c_bmp;
        mNextPageBitmap = n_bmp;
    }

    byte fingerStatus = 0;// 滑动状态

    Matrix matrix = new Matrix();

    int offsetX = 0;

    boolean openAnimation=true;//是否开启平移动画
    
    @Override
    protected void onDraw(Canvas canvas) {
        openAnimation=BookData.getInstance().getAnimation();
        // 绘制
        canvas.save();
        matrix.reset();
        if (openAnimation&&fingerStatus == 1 && offsetX >= -screenW) {
            offsetX -= 40;
            matrix.setTranslate(offsetX, 0);
            canvas.setMatrix(matrix);
            canvas.drawBitmap(mTempPageBitmap, 0, 0, null);
            canvas.drawBitmap(mCurPageBitmap, screenW, 0, null);
        } else if (openAnimation&&fingerStatus == -1 && offsetX <= screenW) {
            offsetX += 40;
            matrix.setTranslate(offsetX, 0);
            canvas.setMatrix(matrix);
            canvas.drawBitmap(mTempPageBitmap, -screenW, 0, null);
            canvas.drawBitmap(mCurPageBitmap, 0, 0, null);
        } else {
            canvas.drawBitmap(mCurPageBitmap, 0, 0, null);
        }
        canvas.restore();
        if (offsetX <= -screenW || offsetX >= screenW) {
            // 书页切换完毕全部恢复默认参数
            offsetX = 0;
            fingerStatus=0;
        } else {
            this.postInvalidate();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mTempPageBitmap = mCurPageBitmap;//缓存上次的Bitmap
        // 点击时，需要检测当前是否有菜单显示
        if (bookMenuView != null && bookMenuView.isShown()) {
            bookMenuView.checkSettingItem();
            return false;
        }
        // 点击文本区域
        int w = MainActivity.getInstance().getScreenWidth();
        int h = MainActivity.getInstance().getScreenHeight();
        if (event.getX() > w / 3 && event.getX() < 2 * w / 3 && event.getY() < 2 * h / 3) {
            // 弹出菜单
            bookMenuView.show();
            fingerStatus=0;
        } else if (event.getX() < w / 3) {
            // 上一页
            _bpg.prePage();
            fingerStatus = -1;
        } else if (event.getX() > 2 * w / 3) {
            // 下一页
            fingerStatus = 1;
            _bpg.nextPage();
        }

        this.postInvalidate();
        return super.onTouchEvent(event);
    }
}
